#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "CTableUser.h"
#include "CDatabaseManager.h"
#include <QApplication>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    CDatabaseManager::instance()->init(qApp->applicationDirPath() + "/userAuth.db");

    ui->setupUi(this);

    ui->lineEdit_lastlogintime->setText(QDateTime::currentDateTime().toString());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::tst_service_state()
{

}


void MainWindow::on_pushButton_add_row_clicked()
{
    if (0 == ui->comboBox_api_type->currentIndex())
    {
        QVariantMap map;
        //    map.insert("Id",ui->lineEdit_id->text().toInt());
        map.insert("Role",ui->lineEdit_role->text().toInt());
        map.insert("UserName",ui->lineEdit_username->text());
        map.insert("Active",ui->lineEdit_active->text().toInt());
        map.insert("Locked",ui->lineEdit_locked->text().toInt());
        map.insert("LoginType",ui->lineEdit_logintype->text().toInt());
        map.insert("LastLoginTime",ui->lineEdit_lastlogintime->text());


        bool ok = CTableUser::getInstance()->addRow(map);
        if(ok){
            ui->textEdit_record->append("addRow 成功!");
        } else {
            ui->textEdit_record->append("addRow 失败!");
        }
    }
    else
    {
        CUserInfo info;
        info.role = ui->lineEdit_role->text().toInt();
        info.userName = ui->lineEdit_username->text();
        info.active = ui->lineEdit_active->text().toInt();
        info.locked = ui->lineEdit_locked->text().toInt();
        info.loginType = ui->lineEdit_logintype->text().toInt();
        info.lastLoginTime = QDateTime::fromString(ui->lineEdit_lastlogintime->text());
        if(CTableUser::getInstance()->addUser(info)){
            ui->textEdit_record->append("addUser 成功!");
        }else{
            ui->textEdit_record->append("addUser 失败!");
        }
    }
}


void MainWindow::on_pushButton_delete_row_clicked()
{


    if (0 == ui->comboBox_api_type->currentIndex())
    {
         bool ok = CTableUser::getInstance()->deleteRow("Id",ui->lineEdit_id->text()/*.toInt()*/);
         if(ok){
             ui->textEdit_record->append("deleteRow 成功!");
         } else {
             ui->textEdit_record->append("deleteRow 失败!");
         }
    }
    else
    {
        bool ok = CTableUser::getInstance()->deleteUser(ui->lineEdit_id->text());
        if(ok){
            ui->textEdit_record->append("deleteUser 成功!");
        } else {
            ui->textEdit_record->append("deleteUser 失败!");
        }
    }
}


void MainWindow::on_pushButton_update_row_clicked()
{
    if (0 == ui->comboBox_api_type->currentIndex())
    {
        QVariantMap map;
        map.insert("Id",ui->lineEdit_id->text().toInt());
        map.insert("Role",ui->lineEdit_role->text().toInt());
        map.insert("UserName",ui->lineEdit_username->text());
        map.insert("Active",ui->lineEdit_active->text().toInt());
        map.insert("Locked",ui->lineEdit_locked->text().toInt());
        map.insert("LoginType",ui->lineEdit_logintype->text().toInt());
        map.insert("LastLoginTime",ui->lineEdit_lastlogintime->text());

        bool ok = CTableUser::getInstance()->updateRow("Id",ui->lineEdit_id->text().toInt(),map);
        if(ok){
            ui->textEdit_record->append("updateRow 成功!");
        } else {
            ui->textEdit_record->append("updateRow 失败!");
        }
    }
    else
    {
        CUserInfo info;
        info.userId = ui->lineEdit_id->text();
        info.role = ui->lineEdit_role->text().toInt();
        info.userName = ui->lineEdit_username->text();
        info.active = ui->lineEdit_active->text().toInt();
        info.locked = ui->lineEdit_locked->text().toInt();
        info.loginType = ui->lineEdit_logintype->text().toInt();
        info.lastLoginTime = QDateTime::fromString(ui->lineEdit_lastlogintime->text());
        if(CTableUser::getInstance()->updateUser(info)){
            ui->textEdit_record->append("updateUser 成功!");
        }else{
            ui->textEdit_record->append("updateUser 失败!");
        }
    }
}


void MainWindow::on_pushButton_query_row_clicked()
{
    if (0 == ui->comboBox_api_type->currentIndex())
    {
        QSharedPointer<QVariantMap> map = CTableUser::getInstance()->queryRow("Id",ui->lineEdit_id->text().toInt());
        if(!map->isEmpty()){
            ui->textEdit_record->append("updateRow 成功!");
            qDebug() << "row data:" << map;
        } else {
            ui->textEdit_record->append("updateRow 失败!");
        }
    }
    else
    {
        QSharedPointer<CUserInfo> info = CTableUser::getInstance()->userInfo(ui->lineEdit_id->text());
        ui->textEdit_record->append("userInfo");
        ui->textEdit_record->append(QString("id:%1").arg(info->userId));
        ui->textEdit_record->append(QString("role:%1").arg(info->role));
        ui->textEdit_record->append(QString("userName:%1").arg(info->userName));
        ui->textEdit_record->append(QString("Active:%1").arg(info->active));
        ui->textEdit_record->append(QString("locked:%1").arg(info->locked));
        ui->textEdit_record->append(QString("loginType:%1").arg(info->loginType));
        ui->textEdit_record->append(QString("lastLoginTime:%1").arg(info->lastLoginTime.toString()));
    }
}

void MainWindow::on_pushButton_query_column_clicked()
{
    QVariant var = CTableUser::getInstance()->queryColumn("Id",ui->lineEdit_id->text().toInt(),
                                                          ui->lineEdit_column_name->text());
    if(var.isValid()){
         ui->textEdit_record->append("updateRow 成功:" + var.toString());
    }else{
         ui->textEdit_record->append("updateRow 失败!");
    }
}


void MainWindow::on_pushButton_update_column_clicked()
{
    QVariant var;
    switch(ui->comboBox_type->currentIndex()){
    case 0:
        var = ui->lineEdit_column_val->text();
    case 1:
        var = ui->lineEdit_column_val->text().toInt();
    default:
        var = ui->lineEdit_column_val->text();
        break;
    }
    bool ok = CTableUser::getInstance()->updateColumn("Id",ui->lineEdit_id->text().toInt(),
                                                           ui->lineEdit_column_name->text(),
                                                           var);
    if(ok){
         ui->textEdit_record->append("updateColumn 成功!");
    }else{
         ui->textEdit_record->append("updateColumn 失败!");
    }
}


void MainWindow::on_pushButton_clear_clicked()
{
    ui->textEdit_record->clear();
}


void MainWindow::on_pushButton_query_all_clicked()
{
    if (0 == ui->comboBox_api_type->currentIndex())
    {
        QSharedPointer<QList<QVariantMap>> list = CTableUser::getInstance()->queryAll();
        qDebug() << "query all:" << list;
        ui->textEdit_record->append("queryAll 查询到数据行数为:" + QString::number(list->count()));
    }
    else
    {
        QSharedPointer<QList<CUserInfo>> listUser = CTableUser::getInstance()->allUser();
        for (int i = 0; i < listUser->count(); i++) {
            CUserInfo info = listUser->at(i);
            ui->textEdit_record->append(QString("userInfo No:%1").arg(i));
            ui->textEdit_record->append(QString("id:%1").arg(info.userId));
            ui->textEdit_record->append(QString("role:%1").arg(info.role));
            ui->textEdit_record->append(QString("userName:%1").arg(info.userName));
            ui->textEdit_record->append(QString("Active:%1").arg(info.active));
            ui->textEdit_record->append(QString("locked:%1").arg(info.locked));
            ui->textEdit_record->append(QString("loginType:%1").arg(info.loginType));
            ui->textEdit_record->append(QString("lastLoginTime:%1").arg(info.lastLoginTime.toString()));
        }
    }
}


void MainWindow::on_pushButton_get_clicked()
{
    QVariant var;
    switch(ui->comboBox_property->currentIndex()){
    case 0:
        var = CTableUser::getInstance()->role(ui->lineEdit_primary_val->text());
        break;
    case 1:
        var = CTableUser::getInstance()->userName(ui->lineEdit_primary_val->text());
        break;
    case 2:
        var = CTableUser::getInstance()->active(ui->lineEdit_primary_val->text());
        break;
    case 3:
        var = CTableUser::getInstance()->locked(ui->lineEdit_primary_val->text());
        break;
    case 4:
        var = CTableUser::getInstance()->loginType(ui->lineEdit_primary_val->text());
        break;
    case 5:
        var = CTableUser::getInstance()->lastLoginTime(ui->lineEdit_primary_val->text());
        break;
    default:
        break;
    }

    ui->textEdit_record->append("get val:" + var.toString());
}


void MainWindow::on_pushButton_set_clicked()
{
    switch(ui->comboBox_property->currentIndex()){
    case 0:
        CTableUser::getInstance()->setRole(ui->lineEdit_primary_val->text(),ui->lineEdit_property_column_val->text());
        break;
    case 1:
        CTableUser::getInstance()->setUserName(ui->lineEdit_primary_val->text(),ui->lineEdit_property_column_val->text());
        break;
    case 2:
        CTableUser::getInstance()->setActive(ui->lineEdit_primary_val->text(),ui->lineEdit_property_column_val->text());
        break;
    case 3:
        CTableUser::getInstance()->setLocked(ui->lineEdit_primary_val->text(),ui->lineEdit_property_column_val->text());
        break;
    case 4:
        CTableUser::getInstance()->setLoginType(ui->lineEdit_primary_val->text(),ui->lineEdit_property_column_val->text());
        break;
    case 5:
        CTableUser::getInstance()->setLastLoginTime(ui->lineEdit_primary_val->text(),ui->lineEdit_property_column_val->text());
        break;
    default:
        break;
    }
}

